package utils

import (
	"bytes"
	"encoding/json"
	"errors"
	"interfaceTest/constant"
	"interfaceTest/model"
	"net/http"
	"net/http/cookiejar"
	"time"
)

// 督导测试账户
// test1
// e10adc3949ba59abbe56e057f20f883e

func GetSupervisorLoginClient() (*http.Client, error) {
	suffix := "/auth/login"
	url := constant.UrlDomain + suffix
	contentType := "application/json"
	user := &model.AuthLoginRequest{
		UserName: "test1",
		Password: "e10adc3949ba59abbe56e057f20f883e",
	}
	jar, _ := cookiejar.New(nil)
	client := &http.Client{
		Timeout: 3 * time.Second,
		Jar:     jar,
	}
	jsonStr, err := json.Marshal(user)
	if err != nil {
		return nil, err
	}
	resp, err := client.Post(url, contentType, bytes.NewBuffer(jsonStr))
	if err != nil {
		return nil, err
	}
	if resp.StatusCode != http.StatusOK {
		return nil, errors.New("Request failed")
	}
	defer resp.Body.Close()
	return client, nil
}

// 咨询师测试账户
// test4
// e10adc3949ba59abbe56e057f20f883e

func GetCounselorLoginClient() (*http.Client, error) {
	suffix := "/auth/login"
	url := constant.UrlDomain + suffix
	contentType := "application/json"
	user := &model.AuthLoginRequest{
		UserName: "test4",
		Password: "e10adc3949ba59abbe56e057f20f883e",
	}
	jar, _ := cookiejar.New(nil)
	client := &http.Client{
		Timeout: 3 * time.Second,
		Jar:     jar,
	}
	jsonStr, err := json.Marshal(user)
	if err != nil {
		return nil, err
	}
	resp, err := client.Post(url, contentType, bytes.NewBuffer(jsonStr))
	if err != nil {
		return nil, err
	}
	if resp.StatusCode != http.StatusOK {
		return nil, errors.New("Request failed")
	}
	defer resp.Body.Close()
	return client, nil
}

// 管理员测试账户
// admin
// 173afad5992a3f73a472fc09b05b1fb7

func GetAdminLoginClient() (*http.Client, error) {
	suffix := "/auth/login"
	url := constant.UrlDomain + suffix
	contentType := "application/json"
	user := &model.AuthLoginRequest{
		UserName: "admin",
		Password: "173afad5992a3f73a472fc09b05b1fb7",
	}
	jar, _ := cookiejar.New(nil)
	client := &http.Client{
		Timeout: 3 * time.Second,
		Jar:     jar,
	}
	jsonStr, err := json.Marshal(user)
	if err != nil {
		return nil, err
	}
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
	if err != nil {
		return nil, err
	}
	req.Header.Set("Content-Type", contentType)
	resp, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	if resp.StatusCode != http.StatusOK {
		return nil, errors.New("Request failed")
	}
	defer resp.Body.Close()
	return client, nil
}
